<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>订单管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        
        <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">

        <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
  	    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
        
        <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
        <script type="text/javascript" src="/static/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/static/js/xadmin.js"></script>
    </head>
<body style="background-color: #FFF;">
    <div style="padding: 20px">
        <div class="demoTable">
            <form class="layui-form">
                <div class="layui-inline">
                    <input class="layui-input" name="nickname" id="nickname" autocomplete="off" placeholder="搜索昵称">
                </div>
    
                <div class="layui-inline">
                    <input class="layui-input" name="order_no" id="order_no" autocomplete="off" placeholder="搜索订单号">
                </div>

                <button class="layui-btn" type="button" id="seach">搜索</button>
            </form>
        </div>
    
        <table class="layui-hide" id="test" lay-filter="test"></table>
    </div>
 
    <script type="text/html" id="barDemo">
        {{# if(d.status == '已付款'){}}
        <a class="layui-btn layui-btn-xs" lay-event="send">发货</a>
        {{# } }}

        {{# if(d.status !== '已付款'){}}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        {{# } }}
    </script>

    <script>
        var page = 1;
        $(function(){
            if(window.location.href.includes('#!true=')){
                page = window.location.href.split('#!true=')[1];
            }
        })

        layui.use('table', function(){
            var table = layui.table;

            function getList(){
                table.render({
                    elem: '#test'
                    ,url:'{:url("Order/order_data")}'
                    ,toolbar: true // 开启头部工具栏
                    ,defaultToolbar: true
                    ,where: {
                        key: {
                            nickname: $('#nickname').val()
                            ,order_no: $('#order_no').val()
                        }
                    }
                    ,cols: [[
                        {field:'id', title:'ID', fixed: 'left', width: 80, unresize: true, sort: true}
                        ,{field:'order_no', title:'订单号', width: 180}
                        ,{field:'nickname', title:'用户昵称', width: 150}
                        ,{field:'product_name', title:'产品名称', width: 150}
                        ,{field:'sku_name', title:'规格（件）', sort: true, width: 120}
                        ,{field:'total', title:'总价', width: 120}
                        ,{field:'status', title:'状态', width: 100}
                        ,{field:'take_name', title:'收货人', width: 120}
                        ,{field:'take_mobile', title:'收货手机号', width: 120}
                        ,{field:'take_address', title:'收货地址', width: 200}
                        ,{field:'create_time', title:'下单时间', width: 180}
                        ,{field:'pay_time', title:'付款时间', width: 180}
                        ,{field:'send_time', title:'发货时间', width: 180}
                        ,{field:'finish_time', title:'完成时间', width: 180}
                        ,{fixed: 'right', title:'操作', width: 100, toolbar: '#barDemo'}
                    ]]
                    ,limit: 15
                    ,page: {
                        curr: page,
                        hash: true
                    }
                });
            }

            getList()

            $("#seach").on('click',function(){
                getList()
            })
          
            // 监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;

                if(obj.event === 'del'){
                    layer.confirm('真的删除么', function(index){
                        $.post('{:url("Order/order_del")}',{
                            id: obj.data.id
                        },function(res){
                            if(res.code == 400){
                                layer.msg(res.msg, {icon: 2});
                            }else{
                                obj.del();
                                layer.msg(res.msg, {icon: 1});
                            }
                        })

                        layer.close(index);
                    });
                }else if(obj.event == 'send'){
                    layer.confirm('确定已经发货吗？', function(index){
                        $.post('{:url("Order/order_send")}',{
                            id: obj.data.id
                        },function(res){
                            if(res.code == 400){
                                layer.msg(res.msg, {icon: 2});
                            }else{
                                layer.msg(res.msg, {icon: 1, time: 1000}, function(){
                                    window.location.reload()
                                }, 1000);
                            }
                        })

                        layer.close(index);
                    });
                }
            });
        });
    </script>
</body>
</html>